home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
181_01
/
carne_fp.cod
< prev
next >
Wrap
Text File
|
1986-01-07
|
22KB
|
716 lines
Reprinted from: Micro/Systems Journal, Volume 1. No. 5. Nov/Dec 1985
Article Title: "Faster Floating Point Math"
Author: Ted Carnevale
-----------------------------------------------------------------
Copy of back issue may be obtained for $4.50 (foreign $6) from:
Micro/Systems Journal
Box 1192
Mountainside NJ 07092
-----------------------------------------------------------------
Copyright 1986
Micro/Systems Journal, Box 1192, Mountainside NJ 07092
This software is released into the public domain for
non-commercial use only.
-----------------------------------------------------------------
Listing 1
=========
/* fpc.c--tests conversion to/from amd9511 fpp format */
#include fprintf.h
#include c80def.h
#define MESSAGE "\nFloating-point format conversion program")
#define DASHES "\n\n----------------------------------------")
#define MAX 6 /* how many different values to feed
to the format conversion routines */
/* replace the next two functions with
your own format conversion routines as needed */
extern long c2amd(); /* link .rel file containing these */
extern float amd2c(); /* to fpc.rel */
main()
{
int i;
static int sx=10;
float x;
static float dx=1.0;
printf(MESSAGE);
printf(DASHES);
/* a geometric series of positive values */
for (i = 0, x = 100.0; i<=MAX; x /= sx, i++) showbits(x);
printf(DASHES);
/* a geometric series of negative values */
for (i = 0, x = -100.0; i<=MAX; x /= sx, i++) showbits(x);
printf(DASHES);
/* a linear series of positive and negative values */
for (i = -MAX, x=(float)(-MAX); i<=MAX; x += dx, i++) showbits(x);
}
/* show bit patterns used by C80 and AMD FPP floating point formats
to represent the float n */
showbits(n)
float n;
{
int i;
union {
float f;
long l;
} x,z; /* unions are easier to use here than pointers */
long y;
x.f=n;
/* show C80's preconversion bit pattern */
printf("\n\nx = %e,\t\tBCDE = ",x.f);
prntlong(x.l);
/* eliminate next few lines if you just want to check the format
used by your version of C */
/* show AMD formatted data */
y=c2amd(x.f);
printf("\n\t\t\t\t AMD = ");
prntlong(y);
/* convert back to C80's format */
z.f=amd2c(y);
printf("\nz = %e,\t\tBCDE = ",z.f);
prntlong(z.l);
/* end of C80 -> AMD -> C80 format conversion test */
}
/* print bit pattern of a long, starting with the high-order bit
of the most significant byte, working down from left to right */
prntlong(k)
long k;
{
int i;
union {
long l;
char b[4];
} datum;
datum.l=k;
for (i=3; i>=0; i--) {
prntbyt(datum.b[i]);
printf(" ");
}
}
/* print the bit pattern for a byte from left to right,
high order bit first (does the dirty work for prntlong) */
prntbyte(i)
int i;
{
int j;
char bit;
for (j=0x80; j>0; j=j>>1) {
if (i & j) bit='1';
else bit='0';
printf("%c",bit);
}
}
Listing 2
=========
TITLE FLTLB
PAGE 64
; Floating point library
; C/80 3.0 (7/7/83) - (c) 1983 Walter Bilofsky
;MODIFIED 8/84 to use amd9511 for floating point multiply/divide
;and MATHLIB functions as well.
;Replaces modules FLTLIB and MATHLIB
; -- N.T.Carnevale
;
;
;these were gleaned from LIB's listing of FLIBRARY.REL:
;
ENTRY Bf.Bl,Bf.Hc,Bf.Hi,Bf.Hu,Bl.Bf
ENTRY cf.eq,cf.ge,cf.gt,cf.le,cf.lt,cf.ne
ENTRY div10.,mul10.
ENTRY dum_
ENTRY errcod
ENTRY F.add,F.div,F.mul,F.neg,F.not,F.sub
ENTRY facl_,facl_1,facl_2,fac_,fac_1
ENTRY fadd.,fadda.,fcomp.,fdiv_a,fdiv_b,fdiv_c,fdiv_g
ENTRY flneg.,float.,flt.0,flt_pk
ENTRY fmlt_1,fmlt_2
ENTRY Hc.Bf,Hi.Bf,Hu.Bf
ENTRY inxhr.
ENTRY movfm.,movfr.,movmf.,movrf.
ENTRY pushf.,qint.,save_,save_1,sign.,zero.
;
EXTRN c.neg,c.sxt,eq.4,g.,Hc.Bl,Hi.Bl,Hu.Bl,L.add,L.neg
EXTRN llong.,movrm.,neq.4,slong.
;
;this preserves the 15 byte data area revealed by LIB:
DSEG
;
facl_: DB 0
facl_1: DB 0
facl_2: DB 0
fac_: DB 0
fac_1: DB 0
save_: DB 0
fmlt_1: DB 0
fmlt_2: DB 0
dum_: DB 0
save_1: DB 0
errcod: DB 0
fdiv_a: DB 0
fdiv_b: DB 0
fdiv_c: DB 0
fdiv_g: DB 0
;
; CSEG
;
flt_pk: DS 0
F.add: XRA A
JMP Dual
F.sub: MVI A,1
JMP Dual
F.mul: JMP fpmul ;jump to new routines
; MVI A,2
; JMP Dual
F.div: JMP fpdiv
; MVI A,3
Dual: CALL movfr.
Listing 3
=========
Ftab: DW fadd.
DW fsub ;next two addresses not used
; DW fpmul
; DW fpdiv
Listing 4
=========
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;
;
;What follows replaces the original code in the FLTLIB section of
;FLOATLIB.ASM that started with fmult3: and ended just before pophrt:.
;
;
CR EQU 0DH
LF EQU 0AH
BDOS EQU 5
BOOT EQU 0
PSTRNG EQU 9
;
;
;send message to console
PRMSG:
PUSH PSW
PUSH B
PUSH D
PUSH H
MVI C,PSTRNG
CALL BDOS
POP H
POP D
POP B
POP PSW
RET
;
;**************************************************************
;
;
;Port addresses
;
BASE EQU 050H ;base address of Compupro SS1 board
CREG EQU BASE+9 ;location of 9511's control & data ports
DREG EQU BASE+8
;
;
;FPP error codes
;
ERRBITS EQU 1EH ;the status register's error bits
OVRFLO EQU 2 ;overflow
UNRFLO EQU 4 ;underflow
NEGARG EQU 8 ;negative argument to sqrt or log function
DIVZER EQU 10H ;divide by zero
TOOBIG EQU 18H ;arg of asin, acos, or exp too big
;
;
;FPP command codes
;
FMUL EQU 12H
FDIV EQU 13H
XCHF EQU 19H ;swap top two locations in fpp's stack
FSQRT EQU 1
FSIN EQU 2
FCOS EQU 3
FTAN EQU 4
FASIN EQU 5
FACOS EQU 6
FATAN EQU 7
FLOG EQU 8 ;log base 10
FLN EQU 9 ;natural logarithm
FEXP EQU 0AH ;e^x
FPWR EQU 0BH ;x^y
;
;**************************************************************
;**************************************************************
;
;
;note: timing indicated for some of the following
;
;This block converts c80 float in BD to FPP format, then loads it into FPP.
;If out of range for FPP, aborts with warning.
;
;Data format conversion routine C2AMD--
;converts c80's float to amd's fp format.
;Based on suggestions by J.Shook, Electronics Lab, Dept.of Chemistry,
;SUNY Stony Brook
;
;Floating point formats
;----------------------
;C80 stores floats as:
; mantissa sign in C7
; mantissa = 24 bit two's complement in CDE,
; with bit 23 assumed = 1.
; exponent is added to 128 (80H) and stored in B.
; If the number is 0, B=0.
;
;FPP stores floats as:
; mantissa sign in B7
; mantissa = 24 bit two's complement in CDE,
; with bit 23 = 1. However,
; the value 0 is represented by BD=0.
; exponent sign in B6
; exponent in B5-B0 (only 6 bits).
;
;
;Call with value to convert